package francis.sin.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
 * 参考的https://code.google.com/p/rapid-framework/wiki/rapid_jsp_extends
 * @author badqiu
 */
public class OverrideTag extends BodyTagSupport{
	private static final long serialVersionUID = -8379959647039117369L;
	
	private String id;
	
	public void setId(String id) {
		this.id = id;
	}

	@Override
	public int doStartTag() throws JspException {
		return isOverrided() ? SKIP_BODY : EVAL_BODY_BUFFERED;
	}

	@Override
	public int doEndTag() throws JspException {
		if(isOverrided()) {
			return EVAL_PAGE;
		}
		BodyContent b = getBodyContent();
		String varName = TagUtils.getOverrideVariableId(id);
		pageContext.getRequest().setAttribute(varName, b.getString());
		return EVAL_PAGE;
	}

	private boolean isOverrided() {
		String varName = TagUtils.getOverrideVariableId(id);
		return pageContext.getRequest().getAttribute(varName) != null;
	}
	
}